define(function(require, exports, module) {
    'use strict';
    var rk = require('rk');
    require('core/apps/feed/feed');

    $.widget('rk.xsyCrmFeed', $.rk.xsyBase, {
        _create: function() {
            var that = this;

            that._beIniting();

            var arCallbacks = [];
            //crm外部容器纵向滚动事件
            this._listenOuterEvent('onCrmPanelScrollDelay', function(info) {
                info.dir //'x', 'y'是横向或纵向滚动
                info.scrollTop //滚动条向下滚动的距离
                info.scrollBottom //滚动条距离底边的距离
                var delta = that.element.offset().top + that.element.height() - $(window).height();
                if (delta < 200) {
                    for (var i = 0, len = arCallbacks.length; i < len; i++) {
                        var fn = arCallbacks[i];
                        if ($.isFunction(fn)) {
                            fn();
                        }
                    }
                }
            });
            var sysId = rk.querySysId('apps', rk.queryBelong(Math.min(that._getBelongId(), 999), 'name')),
                common = that._getCommonData();
            // console.log('sysId: ', sysId);
            this.element.feed2015detail({
                tabs: [{
                    tabname: rk.i18n('APPS_FEED_MY_FOLLOW'),
                    nothingtpl: 'feed_nothing_me',
                    // url: '/json/sns_smartfeed/activity_feeds.action?systemId=510&itemId=242701',
                    url: '/json/sns_newfeed/activity_feeds.action?systemId=' + sysId + '&itemId=' + this._getDetailId(),
                    // url: '/json/sns_feed/mine.action',
                    update: 'false', //是否进行检查更新的动态
                    prependnew: 'true', //新发表动态的时候，是否在当前tab插入新的动态
                    appendmore: 'true' //当前tab是否拥有显示更多的功能
                }],
                scrollRegister: function(fn) {
                    arCallbacks.push(fn);
                },
                background: true,
                pageBelongId: that._getBelongId(),
                pageDetailId: that._getDetailId(),
                disabled: common && common.hasOwnProperty && common.hasOwnProperty('feedsCanInput') ? !common.feedsCanInput : false
            });
            // 收听【外部】控件广播而来的事件
            that._listenOuterEvent('referDataUpdated', function(arg) {
                that.element.feed2015detail('refresh', arg);
            });

            that._listenOuterEvent('onGroupMemberChange', function(arg) {
                that.element.feed2015detail('refresh', arg);
            });

        }
    });

    $.widget('rk.feed2015detail', $.rk.feed2015, {
        options: {
            tabs: [],
            groupId: 0, //编辑器需要的参数
            hasEditor: true, // 是否显示编辑器
            scrollRegister: $.noop,
            pageSize: 20,
            //events
            // 插件创建
            create: null,
            //数据加载完成
            load: function(evt, arg) {
                if ($.isArray(arg.data.feeds) && arg.data.feeds.length > 0) {
                    arg.panel.filter(':data(rk-feedflow)').feedflow(arg.pend + 'Feeds', arg.data.feeds);
                }
                // arg.panel.next().show();
            },
            //获得了更新的数据
            update: null,
            //查看更多事件
            more: null,
            tplPath: 'platform/widgets/common/xsyCrmFeed/widget.tpl',
            className: 'feed-crm-detail',
            pageBelongId: 0,
            pageDetailId: 0
        },
        _patchData: function(data) {
            var that = this;
            if (this.options.hasEditor && data.currentSystemId) {
                this.$editor.acteditor('option', 'systemInfo', {
                    systemId: data.currentSystemId,
                    groupId: data.currentGroupId,
                    itemId: data.currentItemId,
                });
            }
            if ($.isArray(data.feeds)) {
                data.feeds.forEach(function(feed, i) {
                    feed.pageBelongId = that.options.pageBelongId;
                    feed.pageDetailId = that.options.pageDetailId;
                });
            }
        },
        initEditor: function() {
            var that = this;
            that.$editor = this.element.find('div.rk-avtive-publish textarea');
            if (this.options.hasEditor) {
                that.$editor.acteditor({
                    // groupId: this.options.groupId,
                    success: function(evt, resData, reqData) {
                        var $flow = that.$tabBox.tabs('instance').panels.find('div.js-feed-flow');
                        that._patchData(resData);
                        $flow.feedflow('prependFeeds', resData.feeds);
                    },
                    error: function(evt, data) {
                        $.msgNo(rk.i18n('APPS_FEED_RELEASE_DYNAMIC_FAILURE'));
                    }
                });

                if (that.options.disabled) {
                    that.$editor.acteditor('disable');
                }
            }
        },
        initGoTop: function() {
            var that = this;
            var $gotop = that.$gotop = that.element.find('a.js-gototop');
            $gotop.on('click', function(evt) {
                evt.preventDefault();
                var el = that.element,
                    parent = el.scrollParent();
                parent.scrollTop(parent.scrollTop() + (el.offset().top - 2 * parent.offset().top - $('div.xsyAccountHeader.xsy_su_header').outerHeight()));
                // $('div.xsyAccountHeader.xsy_su_header').parent().hide();
            });
        },
        _create: function() {
            var that = this;
            this._super();
            this.element.addClass(this.options.className);
        }
    });
});
